package com.woniuxy01;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Arrays;

/**
 * @author 作者
 * @create 2025-09-16 9:38
 */
public class Test01 {
    public static void main(String[] args) throws IOException {
//        FileInputStream fis=new FileInputStream("a.txt");
//        int mbyte=-1;
//        while((mbyte=fis.read())!=-1){
//            System.out.print((char)mbyte);
//        }
//        fis.close();
        test01();
        FileInputStream fis=new FileInputStream("a.txt");
        byte[] bytes=new byte[3];
        int length=-1;
        while((length=fis.read(bytes))!=-1){
            System.out.println(new String(bytes,0,length));
        }
        fis.close();
    }

    private static void test07() {
        try(FileInputStream fis = new FileInputStream("a.txt");) {
            byte[] bytes = new byte[2];
            int length = -1;
            while ((length = fis.read(bytes)) != -1) {
                System.out.print(new String(bytes, 0, length));
            }
        } catch (IOException e) {
            e.printStackTrace();
//            throw new RuntimeException(e);
        }
    }

    private static void test06() {
        FileInputStream fis = null;
        try {
            fis = new FileInputStream("a.txt");
            byte[] bytes = new byte[2];
            int length = -1;
            while ((length = fis.read(bytes)) != -1) {
                System.out.print(new String(bytes, 0, length));
            }
        } catch (IOException e) {
//            throw new RuntimeException(e);
            e.printStackTrace();
        } finally {
            if (fis!=null) {
                try {
                    fis.close();
                } catch (IOException e) {
//                    throw new RuntimeException(e);
                    e.printStackTrace();
                }
            }
        }
    }

    private static void test05() throws IOException {
        //        FileInputStream fis=new FileInputStream("a.txt");
//        //一次读一个字节字节数组（2个字节）
//        //参数：字节数组：存储读到的数据
//        //返回值:读取的有效的字节个数
//        byte[] bytes=new byte[2];
//        int length = fis.read(bytes);
//        System.out.println("length:"+length);
//        System.out.println("bytes:"+ Arrays.toString(bytes));
//        System.out.println(new String(bytes,0,length));
//        length = fis.read(bytes);
//        System.out.println("length:"+length);
//        System.out.println("bytes:"+ Arrays.toString(bytes));
//        System.out.println(new String(bytes,0,length));
//        length = fis.read(bytes);
//        System.out.println("length:"+length);
//        System.out.println("bytes:"+ Arrays.toString(bytes));
//        System.out.println(new String(bytes,0,length));
//        fis.close();
        FileInputStream fis = new FileInputStream("a.txt");
        byte[] bytes = new byte[2];
        int length = -1;
        while ((length = fis.read(bytes)) != -1) {
            System.out.print(new String(bytes, 0, length));
        }
        fis.close();
    }

    private static void test04() throws IOException {
        FileInputStream fis = new FileInputStream("a.txt");
        //一次读一个字节
        int mybyte;
        while ((mybyte = fis.read()) != -1) {
            System.out.println((char) mybyte);
        }
        fis.close();
    }

    private static void test03() {
        //try-with-resouces语法糖：方便程序员的代码的简化写法
        //需要调用close方法关闭资源的对象，定义到try()的括号中，try执行结束以后，会自动调用close
        try (FileOutputStream fos = new FileOutputStream("a.txt")) {
            fos.write(97);
            fos.write('b');
            fos.write("cd中文".getBytes());
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println(e.getCause());
        }
    }

    private static void test02() {
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream("a.txt");
            fos.write(97);
            fos.write('b');
            fos.write("cd中文".getBytes());
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("处理Exception");
        } finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    System.out.println("处理close IO-Exception");
                }
            }

        }
    }

    private static void test01() throws IOException {
        //字节流写入数据
        //绝对路径:Windows来说从,磁盘的根目录开始写
//        FileOutputStream fos = new FileOutputStream("d:\\aaa\\a.txt");
        //相对路径:当前项目项目的根目录
        FileOutputStream fos = new FileOutputStream("a.txt");
        //一次写一个字节
        fos.write(97);
        fos.write('b');
        fos.write('c');
        //一次写一个字节数组
        fos.write("中文".getBytes());
        //关闭流
        fos.close();
    }
}
